/*
	Selects a new file into the control specified
*/
function AFU_Select( fid, rootpath )
{
	var ctrlValue = document.getElementById( fid + "_value" );
	var ctrlText = document.getElementById( fid + "_text" );
	var ctrlSelLoc = document.getElementById( fid + "_selectlocation" );
	
	if( ctrlValue == null || ctrlText == null || ctrlSelLoc == null ) return false;

	var amUrl = rootpath;
	amUrl += "admin/Private/Asset/default.aspx?";
	amUrl += "&rootpath=" + ctrlSelLoc.value;
	amUrl += "&browse=yes";
	amUrl += "&controlID=" + fid;
	
	var w = window.open(amUrl, "assetman", "width=900, height=600, resizable=yes", true);
	if( w.focus ) w.focus();
	return false;
}
function AFU_SetValue( fid, v )
{
	var ctrlValue = document.getElementById( fid + "_value" );
	var ctrlText = document.getElementById( fid + "_text" );
	if( ctrlValue == null || ctrlText == null ) return;
	
	ctrlValue.value = v;
	ctrlText.value = ctrlValue.value;
}
function AFU_Clear( fid )
{
	var ctrlValue = document.getElementById( fid + "_value" );
	var ctrlText = document.getElementById( fid + "_text" );
	
	if( ctrlValue == null || ctrlText == null ) return false;

	ctrlValue.value = "";
	ctrlText.value = ctrlValue.value;
	return false;
}
function AFU_UploadMode( fid )
{
	var selectDiv = document.getElementById( fid + "_select" );
	var uploadDiv = document.getElementById( fid + "_upload" );
	var ctrlMode = document.getElementById( fid + "_currentmode" );
	
	if( selectDiv == null || uploadDiv == null || ctrlMode == null ) return false;
	
	selectDiv.style.display = "none";
	uploadDiv.style.display = "inline";
	ctrlMode.value = "upload";
	
	return false;
}
function AFU_SelectMode( fid )
{
	var selectDiv = document.getElementById( fid + "_select" );
	var uploadDiv = document.getElementById( fid + "_upload" );
	var ctrlMode = document.getElementById( fid + "_currentmode" );
	
	if( selectDiv == null || uploadDiv == null || ctrlMode == null ) return false;
	
	selectDiv.style.display = "inline";
	uploadDiv.style.display = "none";
	ctrlMode.value = "select";
	
	return false;
}

/* functionality to hijack the document load and form onsubmit */
var _AFU_OldOnLoad = null;
var _AFU_OldOnSubmit = null;
_AFU_OldOnLoad = window.onload;

function AFU_FormSubmit()
{
	var c = true;
	if( _AFU_OldOnSubmit ) 
	{
		c = _AFU_OldOnSubmit();
		if( c == null ) c = true;
	}
	if( ! c ) return false;
	
	// Get body
	var body = document.body;
	// Show the DIV
	var d = document.createElement("DIV");
	d.className = "AFU_UploadingDIV";
	d.innerHTML = "Please wait, files uploading<br/>";
	d.style.visibility = "hidden";
	
	var i = document.createElement("IMG");
	i.src = "/flux_client/1.0.0/AssetFileUpload/img/progress.gif";
	i.border = 0;
	i.alt = "Please wait whilst your files are uploaded";
	i.vspace = 10;
	d.appendChild(i);
	
	body.appendChild( d );	

	var ww = window.innerWidth;
	var wh = window.innerHeight;
	if( ww == null && wh == null )
	{
		ww = document.body.offsetWidth;
		wh = document.body.offsetHeight;
	}
	
	var ox = window.pageXOffset;
	var oy = window.pageYOffset;
	
	if( ox == null && oy == null )
	{
		ox = document.body.scrollLeft;
		oy = document.body.scrollTop;
	}

	d.style.left = ((ww/2) - (d.offsetWidth/2)) + ox;
	d.style.top = ((wh/2) - (d.offsetHeight/2)) + oy;
	d.style.visibility = "visible";

	return true;
}
function AFU_CaptureFormSubmit()
{
	_AFU_OldOnSubmit = document.forms[0].onsubmit;
	document.forms[0].onsubmit = AFU_FormSubmit;
	if( _AFU_OldOnLoad != null ) _AFU_OldOnLoad();
}

window.onload = AFU_CaptureFormSubmit;